//
//  AppDelegate.m
//  qeegooChat
//
//  Created by heew on 16/1/7.
//  Copyright © 2016年 com.autozi. All rights reserved.
//

#import "AppDelegate.h"
#import "QGLoginViewController.h"
#import "QGTabBarController.h"

@interface AppDelegate () <EMChatManagerDelegate>

@property (nonatomic,strong)UINavigationController *loginNav;
@end
#define EaseMobAppKey     @"qeegooid#autozi"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //注册环信
    [[EaseMob sharedInstance]registerSDKWithAppKey:EaseMobAppKey apnsCertName:nil otherConfig:@{kSDKConfigEnableConsoleLogger : @(0)}];
    
    [self loginStateChange:nil];
    
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [self.window makeKeyAndVisible];
    
    //观察登录结果
    [QGNotificationCenter addObserver:self selector:@selector(loginStateChange:) name:QGUserCenterLoginEaseMobSuccessNoti object:nil];
    [QGNotificationCenter addObserver:self selector:@selector(loginStateChange:) name:QGUserCenterLoginOffEaseMobSuccessNoti object:nil];
    return YES;

}


- (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    [SVProgressHUD showSuccessWithStatus:@"自动登录成功"];
    [QGNotificationCenter postNotificationName:QGUserCenterLoginOffEaseMobSuccessNoti object:@(1)];
}

- (void)loginStateChange:(NSNotification *)notice
{
    
    BOOL loginSucces = [notice.object boolValue];
    if (loginSucces) {
        QGTabBarController *tabBar = [[QGTabBarController alloc]init];
        self.window.rootViewController = tabBar;
    }else {
        QGLoginViewController *loginVC = [[QGLoginViewController alloc]init];
        loginVC.title = @"中驰内部IM工具";
        self.loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        UIImage *navigationBarBack = [UIImage imageWithColor:QGMainThemeColor size:CGSizeMake(SCREEN_WIDTH * QGScreenScale, 64.f * QGScreenScale)];
        [self.loginNav.navigationBar setBackgroundImage:navigationBarBack forBarMetrics:UIBarMetricsDefault];
        
        self.window.rootViewController = self.loginNav;
    }
    
}



@end
